// pages/test/myTestResult/index.js
const { $Toast } = require('../../../dist/base/index');
var App = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
		result:'',
		title:'',
		api:App.api,
		id:0
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
		var id = options.id;
		console.log(id);
		var that = this;
		that.setData({
			id:id
		})
		that.getList(id);
  },
	getList:function(id){
		var that = this;
		App.ajax.ajax('userInfo.do?method=testScheduleRec',{
			JSESSIONID: wx.getStorageSync('JSESSIONID'),
			id:id,
		},function(res){
			console.log(res.data.result)
			for (var i = 0; i < res.data.result.length;i++){
				for (var j = 0; j < res.data.result[i].actRec.length;j++){
					res.data.result[i].actRec[j].state=0;
				
				}
			}
			 that.setData({
				 result: res.data.result,
			 })
		})
	},
    //点击选择训练方法
    selectType: function (e) {
        var that = this;
		var id = e.currentTarget.dataset.father;
        var index = e.currentTarget.dataset.index;
        var result = that.data.result;
         console.log(id);
         console.log(index);
		var state = result[id].actRec[index].state;
       if(state == 0){
		   result[id].actRec[index].state = 1;
       }
       else if(state==1){
		   result[id].actRec[index].state = 0
       }
		
        that.setData({
            result:result
        })
    },
    nextStep:function(){
		var that = this;
        var result = that.data.result;
		var all = [];
		for(var i=0;i<result.length;i++){
			for (var j = 0; j < result[i].actRec.length;j++){
				if (result[i].actRec[j].state == 1){
					all.push(result[i].actRec[j].actionId);
				}
			}
			
		}
		var num = all.join(",");
		console.log(num)
		if(all.length==0){
			wx.showToast({
				title: '请选择训练方法',
				icon:'none'
			})
		}
		else{
			App.ajax.ajax('testUserSchedule.do?method=saveTestSub',{
				JSESSIONID: wx.getStorageSync('JSESSIONID'),
				actionList: num,
				i_bb_user_test_id: that.data.id
			},function(res){
				wx.showToast({
					title: '获取成功',
					icon: 'success',
					success: function () {
						setTimeout(function () {
							wx.switchTab({
								url: '../index/index',
							})
						}, 1500)
					}
				})
			})
			
		}
    },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})